//============================================================================
// Name        : cpp_ip2host.cpp
// Author      : khs
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <netdb.h>
#include <string.h>
#include <arpa/inet.h>
using namespace std;

int main() {
	string ip;
	struct hostent* Hostent;

	cout << "input ip: ";
	cin >> ip;

	Hostent = gethostbyaddr(ip.c_str(), ip.length(), AF_INET);

	if(Hostent){
		cout << Hostent->h_name << endl;
	}

	struct addrinfo hints;
	struct addrinfo *res;

	hints.ai_family = AF_INET;

	int status = getaddrinfo(ip.c_str(), 0, 0, &res);

	char host[512],port[128];
	status=getnameinfo(res->ai_addr,res->ai_addrlen,host,512,0,0,0);

	freeaddrinfo(res);

	cout << host << endl;

	return 0;
}
